package com.mohe.dangjian.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.model.OtherParty;
import com.mohe.dangjian.service.interfaces.IOtherParty;
import com.mohe.dangjian.utils.Json;
import com.mohe.dangjian.utils.JsonUtil;
import com.mohe.dangjian.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 其他单位党组织Action
 * 
 * @author wxm
 * 
 */
public class OtherPartyAction extends ActionSupport {

	private static final long serialVersionUID = -5022263044060541958L;
	
	private IOtherParty otherPartyServ;
	
	private List<OtherParty> otherPartyList;
	private Page page = new Page();
	private OtherParty otherParty = new OtherParty();
	
	private String flag;
	// 表示从哪跳转到编辑页面，1:主页面/2:查看页面
	private Integer editFrom;

	public IOtherParty getOtherPartyServ() {
		return otherPartyServ;
	}

	public void setOtherPartyServ(IOtherParty otherPartyServ) {
		this.otherPartyServ = otherPartyServ;
	}

	public List<OtherParty> getOtherPartyList() {
		return otherPartyList;
	}

	public void setOtherPartyList(List<OtherParty> otherPartyList) {
		this.otherPartyList = otherPartyList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public OtherParty getOtherParty() {
		return otherParty;
	}

	public void setOtherParty(OtherParty otherParty) {
		this.otherParty = otherParty;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Integer getEditFrom() {
		return editFrom;
	}

	public void setEditFrom(Integer editFrom) {
		this.editFrom = editFrom;
	}

	/**
	 * 获取党组织信息列表
	 * @return
	 */
	public String getPartyInfoListForPage() {
		int count = otherPartyServ.getCount(otherParty);
		page.setCount(count);
		otherPartyList = otherPartyServ.getOtherPartyList(otherParty, page);
		return "partyInfo.jsp";
	}
	
	/**
	 * 分页
	 */
	public void searchPartyList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = otherPartyServ.getCount(otherParty);
		page.setCount(count);
		List<OtherParty> list = otherPartyServ.getOtherPartyList(otherParty, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (OtherParty s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getName())) {
					map.put("name", s.getName());
				} else {
					map.put("name", "");
				}
				if (!StringUtils.isBlank(s.getAddress())) {
					map.put("address", s.getAddress());
				} else {
					map.put("address", "");
				}
				if (!StringUtils.isBlank(s.getArea())) {
					map.put("area", s.getArea());
				} else {
					map.put("area", "");
				}
				if (!StringUtils.isBlank(s.getType())) {
					map.put("type", s.getType());
				} else {
					map.put("type", "");
				}
				if (s.getWorkerNum() != null) {
					map.put("workerNum", s.getWorkerNum().toString());
				} else {
					map.put("workerNum", "");
				}
				if (s.getHasParty() != null && s.getHasParty().equals(1)) {
					map.put("hasParty", "是");
				} else {
					map.put("hasParty", "否");
				}
				if (!StringUtils.isBlank(s.getPartyName())) {
					map.put("partyName", s.getPartyName());
				} else {
					map.put("partyName", "");
				}
				if (s.getIsRegional() != null && s.getIsRegional().equals(1)) {
					map.put("isRegional", "是");
				} else {
					map.put("isRegional", "否");
				}
				if (s.getMemberNum() != null) {
					map.put("memberNum", s.getMemberNum().toString());
				} else {
					map.put("memberNum", "");
				}
				if (!StringUtils.isBlank(s.getLeaderName())) {
					map.put("leaderName", s.getLeaderName());
				} else {
					map.put("leaderName", "");
				}
				if (!StringUtils.isBlank(s.getLeaderPolitical())) {
					map.put("leaderPolitical", s.getLeaderPolitical());
				} else {
					map.put("leaderPolitical", "");
				}
				if (!StringUtils.isBlank(s.getLeaderTel())) {
					map.put("leaderTel", s.getLeaderTel());
				} else {
					map.put("leaderTel", "");
				}
				if (!StringUtils.isBlank(s.getNote())) {
					map.put("note", s.getNote());
				} else {
					map.put("note", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 跳转到编辑界面
	 * @return
	 */
	public String toEditPartyInfo() {
		otherParty = otherPartyServ.getOtherPartyById(otherParty.getTimeKey());
		return "editPartyInfo.jsp";
	}
	
	/**
	 * 更新党员信息
	 */
	public void updatePartyInfo(){
		Json json = new Json();
		otherParty.setCreateDate(new Date());
		boolean result = otherPartyServ.updateOtherParty(otherParty);
		if (result) {
			json.setMessage("编辑成功");
		} else {
			json.setSuccess(true);
			json.setMessage("编辑失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 保存新增党组织
	 */
	public void savePartyInfo() {
		Json json = new Json();
		boolean result = otherPartyServ.saveOtherParty(otherParty);
		if (result) {
			json.setMessage("新增成功");
		} else {
			json.setSuccess(true);
			json.setMessage("新增失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 删除党组织
	 */
	public void delPartyInfo() {
		Json json = new Json();
		boolean result = otherPartyServ.delOtherParty(otherParty.getTimeKey());
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(true);
			json.setMessage("删除失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String toShowPartyInfo(){
		otherParty = otherPartyServ.getOtherPartyById(otherParty.getTimeKey());
		return "showPartyInfo.jsp";
	}
}
